<template>
  <ContentCard>
    <template #header>
      <pageHeaderNew
        :title="title"
        :breadCrumb="breadCrumb"
        :show-back="true"
      ></pageHeaderNew>
    </template>
    <div class="detail-card">
      <div class="card-top">
        <div class="card-top-item">
          <a-avatar :size="64" @click="submitReturn">Arco</a-avatar>
        </div>
        <div class="card-top-center">
          <a-space :direction="'vertical'">
            <a-space>
              <h3>{{ probationDetail?.name }}</h3>
            </a-space>
            <a-space>
              <a-space>
                {{
                  `考核周期：${probationDetail?.cycleStart} 至 ${probationDetail?.cycleEnd}`
                }}
              </a-space>
              <a-space>
                {{ `预警时间：${probationDetail?.earlyWarningTime}` }}
              </a-space>
              <a-space>
                {{ `启动时间：${probationDetail?.startTime}` }}
              </a-space>
            </a-space>
          </a-space>
        </div>
        <div
          class="card-top-right"
          v-if="
            probationDetail.optionStatus >= 10 &&
            probationDetail.optionStatus !== 20 &&
            probationDetail.optionStatus !== 21 &&
            probationDetail.optionStatus !== 12 &&
            probationDetail.optionStatus !== 13 &&
            probationDetail.optionStatus !== 19
          "
        >
          <div
            v-if="
              probationDetail.optionStatus >= 11 &&
              probationDetail.optionStatus !== 20 &&
              probationDetail.optionStatus !== 21
            "
          >
            <div class="big-circle">
              <div class="small-circle">
                <div class="confirmed">已确认</div>
              </div>
            </div>
          </div>
          <div>
            <a-statistic
              title="考核结果"
              :value="probationDetail?.score"
              show-group-separator
              :precision="2"
            />
          </div>
        </div>
      </div>
      <div class="card-bottom">
        <a-steps :current="currentSteps">
          <a-step :value="1">方案新增</a-step>
          <a-step :value="2">方案确认</a-step>
          <a-step :value="3">方案生效</a-step>
          <a-step :value="4">考核启动</a-step>
          <a-step :value="5">员工自评</a-step>
          <a-step :value="6">考核评分</a-step>
          <a-step :value="7">考核结果确认</a-step>
          <a-step :value="8">考核结束</a-step>
        </a-steps>
      </div>
    </div>
    <a-tabs
      v-if="
        applyChangeTemp === 2 || [6, 7].includes(probationDetail?.optionStatus!)
      "
      style="margin-top: 16px"
      :type="'card'"
      :size="'large'"
      hide-content
      v-model:active-key="tabIndex"
    >
      <a-tab-pane :key="1" title="变更前指标明细"> </a-tab-pane>
      <a-tab-pane :key="2" title="变更后指标明细"> </a-tab-pane>
    </a-tabs>
    <!-- 变更前的数据 -->
    <template v-if="applyChangeTemp === 2 && tabIndex === 1">
      <AssessmentIndex
        ref="assessmentIndexRef"
        v-model:data="targetChangeArr"
        :review="probationDetail?.optionStatus"
        :showRowSelection="showRowSelection"
        v-model:returnIndex="returnIndex"
        :onlyShowSelected="onlyShowSelected"
        :returnTemp="returnTemp"
        :args="{
          showAdd: false,
          showOperation: false,
          scorerType: 1,
          showScoreInput: [9, 8].includes(probationDetail?.optionStatus),
          showWeight: false
        }"
      />
    </template>
    <!-- 变更后的数据--提交审核页面 -->
    <template v-if="applyChangeTemp === 2 && tabIndex === 2">
      <AssessmentIndex
        ref="assessmentIndexRef"
        v-model:data="targetChangeArr2"
        :review="probationDetail?.optionStatus"
        :showRowSelection="showRowSelection"
        v-model:returnIndex="returnIndex"
        :onlyShowSelected="onlyShowSelected"
        :returnTemp="returnTemp"
        :args="{
          showAdd: true,
          showOperation: true,
          scorerType: 1,
          showWeight: false
        }"
        v-model:deleted="deleted"
        v-model:deletedScorer="deletedScorer"
      />
    </template>
    <!-- 变更后数据---审核页面 -->
    <template
      v-if="[6, 7].includes(probationDetail?.optionStatus) && tabIndex === 2"
    >
      <AssessmentIndex
        ref="assessmentIndexRef"
        v-model:data="targetChangeArr3"
        :review="probationDetail?.optionStatus"
        :showRowSelection="showRowSelection"
        v-model:returnIndex="returnIndex"
        :onlyShowSelected="onlyShowSelected"
        :returnTemp="returnTemp"
        :args="{
          showAdd: false,
          showOperation: false,
          scorerType: 1,
          showWeight: false
        }"
      />
    </template>
    <template
      v-if="
        applyChangeTemp !== 2 &&
        tabIndex < 2 &&
        appealStemp !== 2 &&
        ![15].includes(probationDetail.optionStatus)
      "
    >
      <AssessmentIndex
        ref="assessmentIndexRef"
        v-model:data="probationDetail"
        :review="probationDetail?.optionStatus"
        :showRowSelection="showRowSelection"
        v-model:returnIndex="returnIndex"
        :onlyShowSelected="onlyShowSelected"
        :returnTemp="returnTemp"
        :args="args"
        v-model:selected="selected"
        v-model:deleted="deleted"
        v-model:deletedScorer="deletedScorer"
      />
    </template>
    <!-- 管理端审核申诉 -->
    <template v-if="probationDetail.optionStatus == 15">
      <AssessmentIndex
        ref="assessmentIndexRef"
        v-model:data="appealHandleData"
        :review="probationDetail?.optionStatus"
        :showRowSelection="showRowSelection"
        v-model:returnIndex="returnIndex"
        :onlyShowSelected="onlyShowSelected"
        :returnTemp="returnTemp"
        :args="{
          showAdd: probationDetail?.optionStatus === 4 ? true : false,
          showOperation: probationDetail?.optionStatus === 4 ? true : false,
          scorerType: 1,
          showScoreInput: [9, 8].includes(probationDetail?.optionStatus),
          showReason: probationDetail?.optionStatus === 4 ? true : false,
          showIndexScore: probationDetail?.optionStatus >= 10 ? true : false,
          showAppealHandel: probationDetail?.optionStatus === 15 ? true : false,
          showWeight: false
        }"
        v-model:selected="selected"
        v-model:deleted="deleted"
        v-model:deletedScorer="deletedScorer"
      />
    </template>
    <!-- 提交申诉 -->
    <template v-if="appealStemp === 2">
      <AssessmentIndex
        ref="assessmentIndexRef"
        v-model:data="appealData"
        :review="probationDetail?.optionStatus"
        :showRowSelection="showRowSelection"
        v-model:returnIndex="returnIndex"
        :onlyShowSelected="onlyShowSelected"
        :returnTemp="returnTemp"
        :args="{
          showAdd: probationDetail?.optionStatus === 4 ? true : false,
          showOperation: probationDetail?.optionStatus === 4 ? true : false,
          scorerType: 1,
          showScoreInput: [9, 8].includes(probationDetail?.optionStatus),
          showReason: probationDetail?.optionStatus === 4 ? true : false,
          showIndexScore: probationDetail?.optionStatus >= 10 ? true : false,
          showAppeal: showAppeal,
          showWeight: false
        }"
        v-model:selected="selected"
        v-model:deleted="deleted"
        v-model:deletedScorer="deletedScorer"
      />
    </template>
    <div v-if="[6, 7].includes(probationDetail?.optionStatus)">
      变更原因：
      <p style="border: 1px solid #eee; min-height: 60px; padding: 16px">
        {{
          probationDetail.personnelAssessmentProcessRecord[
            probationDetail.personnelAssessmentProcessRecord.length - 1
          ].content
        }}
      </p>
    </div>
    <a-form
      v-if="
        [5, 19, 20].includes(probationDetail?.optionStatus) && tabIndex === 2
      "
      ref="formRef"
      :model="targetChangeArr2"
      auto-label-width
    >
      <a-form-item
        field="reason"
        label="变更原因"
        :rules="[{ required: true, message: '变更原因必填' }]"
      >
        <a-textarea
          v-model="targetChangeArr2.reason"
          placeholder="变更原因"
          allow-clear
        />
      </a-form-item>
    </a-form>
    <a-form
      v-if="returnTemp === 2"
      ref="formRef"
      :model="returnIndex"
      layout="vertical"
      auto-label-width
    >
      <a-form-item
        field="returnReason"
        label="退回原因"
        :rules="[{ required: true, message: '申请原因必填' }]"
      >
        <a-textarea
          v-model="returnIndex.returnReason"
          placeholder="申请原因"
          allow-clear
        />
      </a-form-item>
    </a-form>

    <div v-if="probationDetail?.optionStatus === 4">
      退回原因：
      <p style="border: 1px solid #eee; min-height: 60px; padding: 16px">
        {{
          probationDetail.personnelAssessmentProcessRecord[
            probationDetail.personnelAssessmentProcessRecord.length - 1
          ].content
        }}
      </p>
    </div>
    <ExamineRecord :record="probationDetail.personnelAssessmentProcessRecord" />

    <a-affix :offsetBottom="0" target="#scroll-box">
      <a-space class="btn-group" style="background-color: white">
        <template v-if="showReturnButton">
          <template v-if="returnTemp === 0">
            <!-- <a-button @click="cancel">取消</a-button> -->
            <a-button type="outline" @click="returned" status="warning">
              申请退回
            </a-button>
            <a-button type="primary" @click="confirm">确认</a-button>
          </template>
          <template v-if="returnTemp === 1">
            <a-button @click="cancelReturn">取消</a-button>
            <a-button type="primary" @click="returnedConfirm"
              >确认退回</a-button
            >
          </template>
          <template v-if="returnTemp === 2">
            <a-button @click="cancelSubmit">取消</a-button>
            <a-button type="primary" @click="submitReturn">提交退回</a-button>
          </template>
        </template>
        <template v-if="showScoreButton">
          <!-- <a-button @click="cancel">取消</a-button> -->
          <!-- <a-button type="primary" @click="confirm">暂存</a-button> -->
          <a-button type="primary" @click="submitScore">提交评分</a-button>
        </template>
        <template v-if="showApplyChangeButton">
          <template v-if="applyChangeTemp === 0">
            <!-- <a-button>取消</a-button> -->
            <a-button type="outline" @click="applyChange" status="warning">
              申请变更
            </a-button>
          </template>
          <template v-if="applyChangeTemp === 1">
            <a-button @click="applyCancel">取消</a-button>
            <a-button type="primary" @click="confirmChange">确认变更</a-button>
          </template>
          <template v-if="applyChangeTemp === 2">
            <a-button @click="applyCancel">取消</a-button>
            <a-button type="primary" @click="submitChange">提交变更</a-button>
          </template>
        </template>
        <template v-if="showChangeHandleButton">
          <a-button type="primary" @click="reviewChangeVisible = true"
            >确认变更</a-button
          >
          <a-button type="primary" status="danger" @click="rejectChange"
            >拒绝变更</a-button
          >
        </template>
        <template v-if="submitOrRejectButton">
          <a-button type="primary" @click="submitNewExamin">提交方案</a-button>
          <a-button
            type="primary"
            status="danger"
            @click="rejectReturnVisible = true"
            >拒绝退回</a-button
          >
        </template>
        <template
          v-if="
            probationDetail?.optionStatus === 10 &&
            probationDetail.resultConfirmer === userId
          "
        >
          <a-button type="primary" @click="confirmResult">确认结果</a-button>
          <a-button
            type="primary"
            status="danger"
            @click="confirmResultVisible = true"
            >退回结果</a-button
          >
        </template>
        <template
          v-if="
            [11].includes(probationDetail?.optionStatus) &&
            probationDetail.userId === userId
          "
        >
          <template v-if="appealStemp === 0">
            <a-button type="outline" @click="resultAppeal" status="warning">
              结果申诉
            </a-button>
          </template>
          <template v-if="appealStemp === 1">
            <a-button @click="cancelAppeal"> 取消 </a-button>
            <a-button type="primary" @click="confirmAppeal">
              确认申诉
            </a-button>
          </template>
          <template v-if="appealStemp === 2">
            <a-button @click="cancelAppeal"> 取消 </a-button>
            <a-button type="primary" @click="submitAppeal"> 提交申诉 </a-button>
          </template>
        </template>
        <template
          v-if="
            [15].includes(probationDetail?.optionStatus) &&
            probationDetail.userId !== userId
          "
        >
          <a-button type="primary" @click="submitAppealHandle">
            提交申诉
          </a-button>
        </template>
      </a-space>
    </a-affix>
    <RejectModal
      v-model:visible="rejectVisible"
      v-model:rejectReason="probationDetail.reason"
      @confirmRejecct="confirmRejecct"
    />
    <a-modal
      v-model:visible="reviewChangeVisible"
      @ok="reviewConfirmChange"
      @cancel="reviewChangeVisible = false"
    >
      <template #title> 考核确认 </template>
      <div>是否确认当前考核指标变更，确认后变为考核方案！</div>
    </a-modal>
    <a-modal
      v-model:visible="rejectReturnVisible"
      @ok="rejectReturn"
      @cancel="rejectReturnVisible = false"
    >
      <template #title> 拒绝退回 </template>
      <a-form :model="rejectReturnProbation">
        <a-form-item
          field="reason"
          label="拒绝原因"
          :rules="[{ required: true, message: '退回原因必填' }]"
        >
          <a-textarea
            v-model="rejectReturnProbation.reason"
            placeholder="退回原因"
            allow-clear
          />
        </a-form-item>
      </a-form>
    </a-modal>
    <a-modal
      v-model:visible="confirmResultVisible"
      @ok="returnScoreResult"
      @cancel="confirmResultVisible = false"
    >
      <template #title> 退回结果 </template>
      <a-form :model="probationDetail">
        <a-form-item
          field="reason"
          label="退回原因"
          :rules="[{ required: true, message: '退回原因必填' }]"
        >
          <a-textarea
            v-model="probationDetail.reason"
            placeholder="退回原因"
            allow-clear
          />
        </a-form-item>
      </a-form>
    </a-modal>
  </ContentCard>
</template>
<script lang="ts" setup>
import {
  GetProbationDetails,
  probationConfirm,
  probationChange,
  probationScoreSet,
  probationExamineReview,
  delExamineToolInfo,
  PutProbationExamine,
  editProbationExamine,
  probationResultConfirm,
  probationResultAppealOperate,
  probationResultAppeal,
  delScoreHuman,
  delKeyResult
} from '@/apis/human/base/variationManager/probation';
import { useRoute } from 'vue-router';
import type {
  PersonnelVetoIndexLibraryApply,
  PersonnelKeyResultsIndexLibraryApply,
  PersonnelTargetIndexLibraryApply,
  PersonnelAbilityQualityIndexLibraryApply,
  PersonnelKeyPerformanceIndexLibraryApply,
  PersonnelAddSubtractIndexLibraryApply,
  PersonnelWorkPlanIndexLibraryApply,
  ProbationExamine,
  ProbationScore,
  ReturnIndex
} from '@/apis/human/base/variationManager/probation/type';
import AssessmentIndex from '@/views/Management/Performance/PerformanceManagement/Probation/Components/AssessmentPlan/conponents/AssessmentIndex.vue';
import { cloneDeep } from 'lodash';
import RejectModal from './components/RejectModal.vue';
import ExamineRecord from './components/ExamineRecord.vue';
import { Modal } from '@arco-design/web-vue';

const route = useRoute();
const title = '试用期考核详情';
const breadCrumb: string[] = ['首页', '我的绩效', '试用期考核详情'];
onMounted(() => {
  init();
});
const probationId = ref(0);
const init = () => {
  probationId.value = Number(route.query.id);
  returnIndex.id = probationId.value;
  getProbation();
};
/**
 * 当前登录者是否是考核评分人（不包括自评）
 */
const currentUserIsScorer = ref(false);
const probationDetail = ref<ProbationExamine>({ reason: '' });
const targetChangeArr3 = ref<ProbationExamine>();
const appealHandleData = ref<ProbationExamine>();
//获取试用期方案详情
const getProbation = async () => {
  const res = await GetProbationDetails({ id: probationId.value });
  probationDetail.value = { ...res, reason: '' };
  //所有评分设置为undefind
  probationDetail.value?.personnelTargetIndexLibrary?.forEach((target) => {
    target?.personnelKeyResultsIndexLibrary?.forEach((keyResult) => {
      keyResult?.scorer?.forEach((scorer) => {
        if (!scorer.operator) scorer.score = void 0;
        if (scorer.userId === userId && scorer.type !== 5) {
          currentUserIsScorer.value = true;
        }
      });
    });
  });

  probationDetail.value?.personnelKeyPerformanceIndexLibrary?.forEach(
    (keyPerformance) => {
      let flag = false; //录入值是否被填写
      keyPerformance?.scorer?.forEach((scorer) => {
        if (!scorer.operator) scorer.score = void 0;
        else {
          if (scorer.type !== 5) {
            flag = true;
          }
        }
        if (scorer.userId === userId && scorer.type !== 5) {
          currentUserIsScorer.value = true;
        }
      });
      if (!flag) {
        keyPerformance.inputValue = void 0;
      }
    }
  );
  probationDetail.value?.personnelAbilityQualityIndexLibrary?.forEach(
    (item) => {
      item?.scorer?.forEach((scorer) => {
        if (!scorer.operator) scorer.score = void 0;
        if (scorer.userId === userId && scorer.type !== 5) {
          currentUserIsScorer.value = true;
        }
      });
    }
  );
  probationDetail.value?.personnelWorkPlanIndexLibrary?.forEach((item) => {
    item?.scorer?.forEach((scorer) => {
      if (!scorer.operator) scorer.score = void 0;
      if (scorer.userId === userId && scorer.type !== 5) {
        currentUserIsScorer.value = true;
        console.log('spspsps');
      }
    });
  });
  probationDetail.value?.personnelAddSubtractIndexLibrary?.forEach((item) => {
    item?.scorer?.forEach((scorer) => {
      if (!scorer.operator) scorer.score = void 0;
      if (scorer.userId === userId && scorer.type !== 5) {
        currentUserIsScorer.value = true;
      }
    });
  });
  probationDetail.value?.personnelVetoIndexLibrary?.forEach((item) => {
    item?.scorer?.forEach((scorer) => {
      if (!scorer.operator) {
        scorer.score = void 0;
        scorer.whetherViolate = void 0;
      }
      if (scorer.userId === userId && scorer.type !== 5) {
        currentUserIsScorer.value = true;
      }
    });
  });
  rejectReturnProbation.value = { ...cloneDeep(res), reason: '' };
  targetChangeArr3.value = cloneDeep(res);
  if (res.optionStatus && [6, 7].includes(res.optionStatus)) {
    // 变更申请的数据合并到变更前的数据得到变更后的数据
    res.personnelTargetIndexLibraryApply?.forEach((target) => {
      const { primitiveId } = target;
      target.personnelKeyResultsIndexLibrary =
        target.personnelKeyResultsIndexLibraryApply;
      const targetIndex =
        targetChangeArr3.value?.personnelTargetIndexLibrary?.findIndex(
          (item) => item.id === primitiveId
        );
      if (typeof targetIndex === 'number' && targetIndex < 0) {
        targetChangeArr3.value?.personnelTargetIndexLibrary?.push(target);
      } else {
        targetChangeArr3.value.personnelTargetIndexLibrary[targetIndex] =
          target;
        targetChangeArr3.value.personnelTargetIndexLibrary[
          targetIndex
        ].personnelKeyResultsIndexLibrary =
          target.personnelKeyResultsIndexLibraryApply;
      }
    });
    res.personnelKeyPerformanceIndexLibraryApply?.forEach((item) => {
      const { primitiveId } = item;
      const targetIndex =
        targetChangeArr3.value?.personnelKeyPerformanceIndexLibrary?.findIndex(
          (item) => item.id === primitiveId
        );
      if (targetIndex < 0) {
        targetChangeArr3.value.personnelKeyPerformanceIndexLibrary.push(item);
      } else {
        targetChangeArr3.value.personnelKeyPerformanceIndexLibrary[
          targetIndex
        ] = item;
      }
    });
    res.personnelAbilityQualityIndexLibraryApply?.forEach((item) => {
      const { primitiveId } = item;
      item.personnelAbilityQualityIndexLibraryScoringStandardApply.forEach(
        (apply) => {
          apply.id = apply.primitiveId;
        }
      );
      item.personnelAbilityQualityIndexLibraryScoringStandard =
        item.personnelAbilityQualityIndexLibraryScoringStandardApply;
      const targetIndex =
        targetChangeArr3.value.personnelAbilityQualityIndexLibrary.findIndex(
          (item) => item.id === primitiveId
        );
      if (targetIndex < 0) {
        targetChangeArr3.value.personnelAbilityQualityIndexLibrary.push(item);
      } else {
        targetChangeArr3.value.personnelAbilityQualityIndexLibrary[
          targetIndex
        ] = item;
      }
    });
    res.personnelWorkPlanIndexLibraryApply?.forEach((item) => {
      const { primitiveId } = item;
      const targetIndex =
        targetChangeArr3.value.personnelWorkPlanIndexLibrary.findIndex(
          (item) => item.id === primitiveId
        );
      if (targetIndex < 0) {
        targetChangeArr3.value.personnelWorkPlanIndexLibrary.push(item);
      } else {
        targetChangeArr3.value.personnelWorkPlanIndexLibrary[targetIndex] =
          item;
      }
    });
    res.personnelVetoIndexLibraryApply?.forEach((item) => {
      const { primitiveId } = item;
      const targetIndex =
        targetChangeArr3.value.personnelVetoIndexLibrary.findIndex(
          (item) => item.id === primitiveId
        );
      if (targetIndex < 0) {
        targetChangeArr3.value.personnelVetoIndexLibrary.push(item);
      } else {
        targetChangeArr3.value.personnelVetoIndexLibrary[targetIndex] = item;
      }
    });
    res.personnelAddSubtractIndexLibraryApply?.forEach((item) => {
      const { primitiveId } = item;
      const targetIndex =
        targetChangeArr3.value.personnelAddSubtractIndexLibrary.findIndex(
          (item) => item.id === primitiveId
        );
      if (targetIndex < 0) {
        targetChangeArr3.value.personnelAddSubtractIndexLibrary.push(item);
      } else {
        targetChangeArr3.value.personnelAddSubtractIndexLibrary[targetIndex] =
          item;
      }
    });
  }
  if (probationDetail.value.optionStatus === 15) {
    appealHandleData.value = cloneDeep(probationDetail.value);
    const {
      personnelTargetIndexLibrary,
      personnelKeyPerformanceIndexLibrary,
      personnelWorkPlanIndexLibrary,
      personnelAddSubtractIndexLibrary,
      personnelVetoIndexLibrary,
      personnelAbilityQualityIndexLibrary
    } = appealHandleData.value;
    appealHandleData.value.personnelTargetIndexLibrary =
      personnelTargetIndexLibrary?.filter((target) => target.state === 4);
    appealHandleData.value.personnelKeyPerformanceIndexLibrary =
      personnelKeyPerformanceIndexLibrary?.filter(
        (target) => target.state === 4
      );
    appealHandleData.value.personnelWorkPlanIndexLibrary =
      personnelWorkPlanIndexLibrary?.filter((target) => target.state === 4);
    appealHandleData.value.personnelAddSubtractIndexLibrary =
      personnelAddSubtractIndexLibrary?.filter((target) => target.state === 4);
    appealHandleData.value.personnelAbilityQualityIndexLibrary =
      personnelAbilityQualityIndexLibrary?.filter(
        (target) => target.state === 4
      );
    appealHandleData.value.personnelVetoIndexLibrary =
      personnelVetoIndexLibrary?.filter((target) => target.state === 4);
    console.log('appealHandleData.value', appealHandleData.value);
  }
  reset();
};
const currentSteps = computed(() => {
  if (!probationDetail.value) return 0;
  const { optionStatus } = probationDetail.value;
  if (!optionStatus) return 0;

  if ([3, 4, 21].includes(optionStatus)) {
    return 2; //方案确认
  }
  if ([5, 19, 20].includes(optionStatus)) {
    return 3; //方案生效
  }
  if ([6, 7].includes(optionStatus)) {
    return 4; //方案启动
  }
  if ([8, 12].includes(optionStatus)) {
    return 5; //员工自评
  }
  if ([9, 13].includes(optionStatus)) {
    return 6; //考核评分
  }
  if ([10, 11, 15].includes(optionStatus)) {
    return 7; //方案结果确认
  }
  if (optionStatus === 16) {
    return 8; //考核结束
  }
  if (optionStatus === 17) {
    return 9; //考核结束
  }
  return 0;
});
//退回的各指标
const returnIndex = reactive<ReturnIndex>({
  id: 0,
  optionStatus: 0,
  returnReason: '',
  personnelAbilityQualityIndexLibrary: [],
  personnelAddSubtractIndexLibrary: [],
  personnelKeyPerformanceIndexLibrary: [],
  personnelVetoIndexLibrary: [],
  personnelWorkPlanIndexLibrary: [],
  personnelTargetIndexLibrary: []
});
const showRowSelection = ref(false);
const returned = () => {
  showRowSelection.value = !showRowSelection.value;
  returnTemp.value = 1;
};
const onlyShowSelected = ref(false);
const returnedConfirm = () => {
  returnTemp.value++;
  onlyShowSelected.value = true;
  showRowSelection.value = !showRowSelection.value;
};
const returnTemp = ref(0); //控制显示申请退回，确认退回，提交退回按钮显示用
const cancelReturn = () => {
  showRowSelection.value = !showRowSelection.value;
  returnTemp.value--;
};
const formRef = ref();
const submitReturn = async () => {
  const check = await formRef.value.validate();
  if (check) return;
  Modal.info({
    title: '确认提交',
    hideCancel: false,
    content: '',
    onCancel: () => {
      console.log('onCancel');
    },
    onOk: async () => {
      returnIndex.optionStatus = 4;
      probationDetail.value?.personnelTargetIndexLibrary?.forEach((target) => {
        const { id, returnReason } = target;
        if (!selected.value.targetIds.includes(id)) return;

        let targetObj: any = { id, state: 2, returnReason };
        returnIndex.personnelTargetIndexLibrary.push(targetObj);
      });
      probationDetail.value?.personnelKeyPerformanceIndexLibrary?.forEach(
        (keyPerformance) => {
          const { id, returnReason } = keyPerformance;
          if (!selected.value.keyPerformanceIds.includes(id)) return;
          let targetObj: any = { id, state: 2, returnReason };
          returnIndex.personnelKeyPerformanceIndexLibrary.push(targetObj);
        }
      );
      probationDetail.value?.personnelAbilityQualityIndexLibrary?.forEach(
        (ability) => {
          const { id, returnReason } = ability;
          if (!selected.value.ablityIds.includes(id)) return;
          let targetObj: any = { id, state: 2, returnReason };
          returnIndex.personnelAbilityQualityIndexLibrary.push(targetObj);
        }
      );
      probationDetail.value?.personnelAddSubtractIndexLibrary?.forEach(
        (item) => {
          const { id, returnReason } = item;
          if (!selected.value.addSubtractIds.includes(id)) return;

          let targetObj: any = { id, state: 2, returnReason };
          returnIndex.personnelAddSubtractIndexLibrary.push(targetObj);
        }
      );
      probationDetail.value?.personnelVetoIndexLibrary?.forEach((item) => {
        const { id, returnReason } = item;
        if (!selected.value.vetoIds.includes(id)) return;

        let targetObj: any = { id, state: 2, returnReason };
        returnIndex.personnelVetoIndexLibrary.push(targetObj);
      });
      probationDetail.value?.personnelWorkPlanIndexLibrary?.forEach((item) => {
        const { id, returnReason } = item;
        if (!selected.value.workPlanIds.includes(id)) return;

        let targetObj: any = { id, state: 2, returnReason };
        returnIndex.personnelWorkPlanIndexLibrary.push(targetObj);
      });
      await probationConfirm(returnIndex);
      getProbation();
    }
  });
};
const cancelSubmit = () => {
  onlyShowSelected.value = false;
  showRowSelection.value = !showRowSelection.value;
  returnTemp.value--;
};
const confirm = async () => {
  Modal.info({
    title: '确认方案',
    hideCancel: false,
    content: '',
    onCancel: () => {
      console.log('onCancel');
    },
    onOk: async () => {
      returnIndex.optionStatus = 5;
      probationDetail.value?.personnelTargetIndexLibrary?.forEach((target) => {
        const { id } = target;
        let targetObj: any = { id, state: 1 };
        returnIndex.personnelTargetIndexLibrary.push(targetObj);
      });
      await probationConfirm(returnIndex);
      getProbation();
    }
  });
};
// 提交评分
const submitScore = async () => {
  Modal.info({
    title: '确认提交',
    hideCancel: false,
    content: '',
    onCancel: () => {
      console.log('onCancel');
    },
    onOk: async () => {
      returnIndex.optionStatus = 10;
      let scorerObj: ProbationScore = {
        id: probationId.value,
        optionStatus: probationDetail.value.optionStatus,
        list: [],
        indexWeight: []
      };
      probationDetail.value?.personnelTargetIndexLibrary?.forEach((target) => {
        if (target.personnelKeyResultsIndexLibrary) {
          target.personnelKeyResultsIndexLibrary.forEach((targetItem) => {
            targetItem?.scorer?.forEach((scorer) => {
              if (scorer.score === void 0) return;
              const { id, score, note } = scorer;
              let scorerInfo = { id, type: 6, score, note };
              scorerObj.list?.push(scorerInfo);
            });
          });
        }
      });
      probationDetail.value?.personnelKeyPerformanceIndexLibrary?.forEach(
        (target) => {
          let temp = { id: target.id, inputValue: target.inputValue };
          scorerObj.indexWeight?.push(temp);
          if (target.scorer) {
            target?.scorer?.forEach((scorer) => {
              if (scorer.score === void 0) return;
              const { id, score, note } = scorer;
              let scorerInfo = { id, type: 3, score, note };
              scorerObj.list?.push(scorerInfo);
            });
          }
        }
      );
      probationDetail.value?.personnelAddSubtractIndexLibrary?.forEach(
        (target) => {
          if (target.scorer) {
            target?.scorer?.forEach((scorer) => {
              if (scorer.score === void 0) return;
              const { id, score, note } = scorer;
              let scorerInfo = { id, type: 2, score, note };
              scorerObj.list?.push(scorerInfo);
            });
          }
        }
      );
      if (probationDetail.value?.optionStatus === 9) {
        probationDetail.value?.personnelVetoIndexLibrary?.forEach((target) => {
          if (target.scorer) {
            target?.scorer?.forEach((scorer) => {
              if (
                scorer.score === void 0 &&
                typeof scorer.whetherViolate === void 0
              )
                return;
              console.log(111);

              const { id, score, note, whetherViolate } = scorer;
              let scorerInfo = { id, type: 4, score, note, whetherViolate };
              scorerObj.list?.push(scorerInfo);
            });
          }
        });
      }

      probationDetail.value?.personnelAbilityQualityIndexLibrary?.forEach(
        (target) => {
          if (target.scorer) {
            target?.scorer?.forEach((scorer) => {
              if (scorer.score === void 0) return;
              const { id, score, note } = scorer;
              let scorerInfo = { id, type: 1, score, note };
              scorerObj.list?.push(scorerInfo);
            });
          }
        }
      );
      probationDetail.value?.personnelWorkPlanIndexLibrary?.forEach(
        (target) => {
          if (target.scorer) {
            target?.scorer?.forEach((scorer) => {
              if (scorer.score === void 0) return;
              const { id, score, note } = scorer;
              let scorerInfo = { id, type: 5, score, note };
              scorerObj.list?.push(scorerInfo);
            });
          }
        }
      );
      // console.log('scorerObj', scorerObj);
      // return;
      await probationScoreSet(scorerObj);
      getProbation();
    }
  });
};
const applyChangeTemp = ref(0); //变更申请处于第几步骤
const applyChange = () => {
  showRowSelection.value = !showRowSelection.value;
  applyChangeTemp.value = 1;
};
const targetChangeArr: any = ref(); //申请变更前的数据
const targetChangeArr2 = ref<ProbationExamine>({ reason: '' }); //申请变更后的数据
//确认变更
const confirmChange = () => {
  applyChangeTemp.value++;
  showRowSelection.value = !showRowSelection.value;
  targetChangeArr.value = { ...cloneDeep(probationDetail.value) };
  targetChangeArr.value.personnelTargetIndexLibrary = [];
  targetChangeArr.value.personnelAbilityQualityIndexLibrary = [];
  targetChangeArr.value.personnelAddSubtractIndexLibrary = [];
  targetChangeArr.value.personnelWorkPlanIndexLibrary = [];
  targetChangeArr.value.personnelVetoIndexLibrary = [];
  targetChangeArr.value.personnelKeyPerformanceIndexLibrary = [];
  probationDetail.value.personnelTargetIndexLibrary?.forEach((item) => {
    if (!item.id) return;
    if (selected.value.targetIds.includes(item.id)) {
      targetChangeArr.value.personnelTargetIndexLibrary.push(item);
    }
  });
  probationDetail.value.personnelAbilityQualityIndexLibrary?.forEach((item) => {
    if (!item.id) return;
    if (selected.value.ablityIds.includes(item.id)) {
      targetChangeArr.value.personnelAbilityQualityIndexLibrary.push(item);
    }
  });
  probationDetail.value.personnelAddSubtractIndexLibrary?.forEach((item) => {
    if (!item.id) return;
    if (selected.value.addSubtractIds.includes(item.id)) {
      targetChangeArr.value.personnelAddSubtractIndexLibrary.push(item);
    }
  });
  probationDetail.value.personnelWorkPlanIndexLibrary?.forEach((item) => {
    if (!item.id) return;
    if (selected.value.workPlanIds.includes(item.id)) {
      targetChangeArr.value.personnelWorkPlanIndexLibrary.push(item);
    }
  });
  probationDetail.value.personnelVetoIndexLibrary?.forEach((item) => {
    if (!item.id) return;
    if (selected.value.vetoIds.includes(item.id)) {
      targetChangeArr.value.personnelVetoIndexLibrary.push(item);
    }
  });
  probationDetail.value.personnelKeyPerformanceIndexLibrary?.forEach((item) => {
    if (!item.id) return;
    if (selected.value.keyPerformanceIds.includes(item.id)) {
      targetChangeArr.value.personnelKeyPerformanceIndexLibrary.push(item);
    }
  });
  targetChangeArr2.value = cloneDeep({
    ...targetChangeArr.value,
    reason: targetChangeArr2.value.reason
  });
};
const applyCancel = () => {
  applyChangeTemp.value--;
  tabIndex.value = 1;
  // onlyShowSelected.value = true;
  showRowSelection.value = !showRowSelection.value;
};
const tabIndex = ref(1);
// 提交变更。
const submitChange = async () => {
  const check = await formRef.value.validate();
  if (check) return;
  Modal.info({
    title: '确认提交',
    hideCancel: false,
    content: '',
    onCancel: () => {
      console.log('onCancel');
    },
    onOk: async () => {
      const copyData: ProbationExamine = cloneDeep(targetChangeArr2.value);
      let changeIndexData: ProbationExamine = {
        ...copyData,
        optionStatus: userId === copyData.userId ? 6 : 7, //6是自己提交，7是管理提交
        personnelTargetIndexLibraryApply: [],
        personnelAbilityQualityIndexLibraryApply: [],
        personnelAddSubtractIndexLibraryApply: [],
        personnelKeyPerformanceIndexLibraryApply: [],
        personnelVetoIndexLibraryApply: [],
        personnelWorkPlanIndexLibraryApply: []
      };
      // 查找增加和修改的指标
      targetChangeArr2.value.personnelTargetIndexLibrary?.forEach((target) => {
        const { id: primitiveId, name, weight } = target;
        let tempTargetData: PersonnelVetoIndexLibraryApply = {
          ...target,
          primitiveId,
          operate: primitiveId ? 3 : 2,
          id: void 0,
          personnelKeyResultsIndexLibraryApply: []
        };
        //成果
        if (Array.isArray(target.personnelKeyResultsIndexLibrary)) {
          target.personnelKeyResultsIndexLibrary?.forEach((keyResult) => {
            const { id: primitiveId } = keyResult;
            let tempKeyResult: any = {
              ...keyResult,
              primitiveId,
              operate: primitiveId ? 3 : 2,
              scorer: [],
              id: void 0,
              updatedAt: void 0,
              operatorInfo: void 0
            };
            // 评分人
            keyResult?.scorer?.forEach((scorer) => {
              const { id: primitiveId } = scorer;
              tempKeyResult.scorer.push({
                ...scorer,
                primitiveId,
                operate: primitiveId ? 3 : 2,
                updatedAt: void 0,
                id: void 0,
                operatorInfo: void 0,
                userInfo: void 0
              });
            });
            tempTargetData.personnelKeyResultsIndexLibraryApply.push(
              tempKeyResult
            );
          });
        }
        changeIndexData.personnelTargetIndexLibraryApply?.push(tempTargetData);
      });
      // 关键业绩考核
      targetChangeArr2.value.personnelKeyPerformanceIndexLibrary?.forEach(
        (item) => {
          const { id: primitiveId } = item;
          let tempData: any = {
            ...item,
            primitiveId,
            operate: primitiveId ? 3 : 2,
            id: void 0,
            scorer: []
          };
          // 评分人
          item?.scorer?.forEach((scorer) => {
            const { id: primitiveId } = scorer;
            tempData.scorer.push({
              ...scorer,
              primitiveId,
              operate: primitiveId ? 3 : 2,
              updatedAt: void 0,
              id: void 0,
              operatorInfo: void 0,
              userInfo: void 0
            });
          });
          changeIndexData.personnelKeyPerformanceIndexLibraryApply?.push(
            tempData
          );
        }
      );

      // 能力素质考核
      targetChangeArr2.value.personnelAbilityQualityIndexLibrary?.forEach(
        (item) => {
          const { id: primitiveId } = item;
          let tempData: any = {
            ...item,
            primitiveId,
            operate: primitiveId ? 3 : 2,
            id: void 0,
            scorer: [],
            personnelAbilityQualityIndexLibraryScoringStandardApply: []
          };
          // 评分人
          item?.scorer?.forEach((scorer) => {
            const { id: primitiveId } = scorer;
            tempData.scorer.push({
              ...scorer,
              primitiveId,
              operate: primitiveId ? 3 : 2,
              updatedAt: void 0,
              id: void 0,
              operatorInfo: void 0,
              userInfo: void 0
            });
          });
          item?.personnelAbilityQualityIndexLibraryScoringStandard?.forEach(
            (item) => {
              const { id: primitiveId } = item;
              tempData.personnelAbilityQualityIndexLibraryScoringStandardApply.push(
                {
                  ...item,
                  primitiveId,
                  operate: primitiveId ? 3 : 2,
                  updatedAt: void 0,
                  id: void 0
                }
              );
            }
          );
          changeIndexData.personnelAbilityQualityIndexLibraryApply?.push(
            tempData
          );
        }
      );
      // 工作计划考核
      targetChangeArr2.value.personnelWorkPlanIndexLibrary?.forEach((item) => {
        const { id: primitiveId } = item;
        let tempData: any = {
          ...item,
          primitiveId,
          operate: primitiveId ? 3 : 2,
          id: void 0,
          scorer: []
        };
        // 评分人
        item?.scorer?.forEach((scorer) => {
          const { id: primitiveId } = scorer;
          tempData.scorer.push({
            ...scorer,
            primitiveId,
            operate: primitiveId ? 3 : 2,
            updatedAt: void 0,
            id: void 0,
            operatorInfo: void 0,
            userInfo: void 0
          });
        });
        changeIndexData.personnelWorkPlanIndexLibraryApply?.push(tempData);
      });
      // 加减分项
      targetChangeArr2.value.personnelAddSubtractIndexLibrary?.forEach(
        (item) => {
          const { id: primitiveId } = item;
          let tempData: any = {
            ...item,
            primitiveId,
            operate: primitiveId ? 3 : 2,
            id: void 0,
            scorer: []
          };
          // 评分人
          item?.scorer?.forEach((scorer) => {
            const { id: primitiveId } = scorer;
            tempData.scorer.push({
              ...scorer,
              primitiveId,
              operate: primitiveId ? 3 : 2,
              updatedAt: void 0,
              id: void 0,
              operatorInfo: void 0,
              userInfo: void 0
            });
          });
          changeIndexData.personnelAddSubtractIndexLibraryApply?.push(tempData);
        }
      );
      // 否决事项
      targetChangeArr2.value.personnelVetoIndexLibrary?.forEach((item) => {
        const { id: primitiveId } = item;
        let tempData: PersonnelVetoIndexLibraryApply = {
          ...item,
          primitiveId,
          operate: primitiveId ? 3 : 2,
          id: void 0,
          scorer: []
        };
        // 评分人
        item?.scorer?.forEach((scorer) => {
          const { id: primitiveId } = scorer;
          tempData.scorer?.push({
            ...scorer,
            primitiveId,
            operate: primitiveId ? 3 : 2,
            updatedAt: void 0,
            id: void 0,
            operatorInfo: void 0,
            userInfo: void 0
          });
        });
        changeIndexData.personnelVetoIndexLibraryApply?.push(tempData);
      });
      //删除的指标
      if (deleted.value.targetIds.length) {
        // 指标
        deleted.value.targetIds.forEach((id) => {
          const target =
            probationDetail.value.personnelTargetIndexLibrary?.find(
              (item) => item.id === id
            );
          if (!target) return;
          const { id: primitiveId, name, weight } = target;
          let tempTargetData: any = {
            ...target,
            primitiveId,
            operate: 4,
            id: void 0,
            personnelKeyResultsIndexLibraryApply: []
          };
          //成果
          if (target && Array.isArray(target.personnelKeyResultsIndexLibrary)) {
            target.personnelKeyResultsIndexLibrary.forEach((keyResult) => {
              const { id: primitiveId } = keyResult;
              let tempKeyResult: PersonnelKeyResultsIndexLibraryApply = {
                ...keyResult,
                primitiveId,
                operate: 4,
                scorer: [],
                id: void 0,
                updatedAt: void 0,
                operatorInfo: void 0
              };
              // 评分人
              keyResult?.scorer.forEach((scorer) => {
                const { id: primitiveId } = scorer;
                tempKeyResult.scorer.push({
                  ...scorer,
                  primitiveId,
                  operate: 4,
                  updatedAt: void 0,
                  id: void 0,
                  operatorInfo: void 0,
                  userInfo: void 0
                });
              });
              tempTargetData.personnelKeyResultsIndexLibraryApply.push(
                tempKeyResult
              );
            });
          }

          changeIndexData.personnelTargetIndexLibraryApply?.push(
            tempTargetData
          );
        });
      }
      if (deleted.value.keyPerformanceIds.length) {
        // 指标
        deleted.value.keyPerformanceIds?.forEach((id) => {
          const item =
            probationDetail.value.personnelKeyPerformanceIndexLibrary?.find(
              (ele) => ele.id === id
            );
          if (!item) return;
          const { id: primitiveId } = item;
          let tempData: any = {
            ...item,
            primitiveId,
            operate: 4,
            id: void 0,
            scorer: []
          };
          // 评分人
          item?.scorer?.forEach((scorer) => {
            const { id: primitiveId } = scorer;
            tempData.scorer.push({
              ...scorer,
              primitiveId,
              operate: 4,
              updatedAt: void 0,
              id: void 0,
              operatorInfo: void 0,
              userInfo: void 0
            });
          });
          changeIndexData.personnelKeyPerformanceIndexLibraryApply?.push(
            tempData
          );
        });
      }
      if (deleted.value.ablityIds.length) {
        // 指标
        deleted.value.ablityIds?.forEach((id) => {
          const item =
            probationDetail.value.personnelAbilityQualityIndexLibrary?.find(
              (ele) => ele.id === id
            );
          if (!item) return;
          const { id: primitiveId } = item;
          let tempData: any = {
            ...item,
            primitiveId,
            operate: 4,
            id: void 0,
            scorer: []
          };
          // 评分人
          item?.scorer?.forEach((scorer) => {
            const { id: primitiveId } = scorer;
            tempData.scorer.push({
              ...scorer,
              primitiveId,
              operate: 4,
              updatedAt: void 0,
              id: void 0,
              operatorInfo: void 0,
              userInfo: void 0
            });
          });
          changeIndexData.personnelAbilityQualityIndexLibraryApply?.push(
            tempData
          );
        });
      }
      if (deleted.value.workPlanIds.length) {
        // 指标
        deleted.value.workPlanIds?.forEach((id) => {
          const item =
            probationDetail.value.personnelWorkPlanIndexLibrary?.find(
              (ele) => ele.id === id
            );
          if (!item) return;
          const { id: primitiveId, name, weight } = item;
          let tempData: any = {
            ...item,
            primitiveId,
            operate: 4,
            id: void 0,
            scorer: []
          };
          // 评分人
          item?.scorer?.forEach((scorer) => {
            const { id: primitiveId } = scorer;
            tempData.scorer.push({
              ...scorer,
              primitiveId,
              operate: 4,
              updatedAt: void 0,
              id: void 0,
              operatorInfo: void 0,
              userInfo: void 0
            });
          });
          changeIndexData.personnelWorkPlanIndexLibraryApply?.push(tempData);
        });
      }
      if (deleted.value.addSubtractIds.length) {
        // 指标
        deleted.value.addSubtractIds?.forEach((id) => {
          const item =
            probationDetail.value.personnelAddSubtractIndexLibrary?.find(
              (ele) => ele.id === id
            );
          if (!item) return;
          const { id: primitiveId, name, weight } = item;
          let tempData: any = {
            ...item,
            primitiveId,
            operate: 4,
            id: void 0,
            scorer: []
          };
          // 评分人
          item?.scorer?.forEach((scorer) => {
            const { id: primitiveId } = scorer;
            tempData.scorer.push({
              ...scorer,
              primitiveId,
              operate: 4,
              updatedAt: void 0,
              id: void 0,
              operatorInfo: void 0,
              userInfo: void 0
            });
          });
          changeIndexData.personnelAddSubtractIndexLibraryApply?.push(tempData);
        });
      }
      if (deleted.value.vetoIds.length) {
        // 指标
        deleted.value.vetoIds?.forEach((id) => {
          const item = probationDetail.value.personnelVetoIndexLibrary?.find(
            (ele) => ele.id === id
          );
          if (!item) return;
          const { id: primitiveId, name, weight } = item;
          let tempData: any = {
            ...item,
            primitiveId,
            operate: 4,
            id: void 0,
            scorer: []
          };
          // 评分人
          item?.scorer?.forEach((scorer) => {
            const { id: primitiveId } = scorer;
            tempData.scorer.push({
              ...scorer,
              primitiveId,
              operate: 4,
              updatedAt: void 0,
              id: void 0,
              operatorInfo: void 0,
              userInfo: void 0
            });
          });
          changeIndexData.personnelVetoIndexLibraryApply?.push(tempData);
        });
      }
      applyChangeTemp.value = 0;
      await probationChange(changeIndexData);
      // probationDetail.value?.optionStatus =
      getProbation();
    }
  });
};
const reviewChangeVisible = ref(false);
//审核确认变更
const reviewConfirmChange = async () => {
  let tempObj: ProbationExamine = cloneDeep(probationDetail.value);
  tempObj.optionStatus = 20;
  tabIndex.value = 1;
  // tempObj.personnelTargetIndexLibrary = [];
  //去掉删除的指标
  let delIndex = {
    targetIds: [],
    veteIds: [],
    workPlanIds: [],
    addSubtractIds: [],
    ablityIds: [],
    keyPerformanceIds: [],
    resultIds: []
  };
  let delScorer = {
    targetIds: [],
    veteIds: [],
    workPlanIds: [],
    addSubtractIds: [],
    ablityIds: [],
    keyPerformanceIds: []
  };
  //收集变更前的数据id；成果，评分人
  tempObj.personnelTargetIndexLibraryApply?.forEach((target) => {
    let tempTargetIndex = tempObj.personnelTargetIndexLibrary.findIndex(
      (item) => item.id === target.primitiveId && item.id !== item.primitiveId
    );
    let tempTarget = tempObj.personnelTargetIndexLibrary[tempTargetIndex];
    if (target && target.operate === 3) {
      //获取变更前数据所有id
      // 修改的时候才对比id
      const beforeResultIds = tempTarget.personnelKeyResultsIndexLibrary?.map(
        (item) => item.id
      );
      beforeResultIds.forEach((id) => {
        const beforeIndex = target.personnelKeyResultsIndexLibrary.findIndex(
          (item) => item.primitiveId === id
        );
        //没有找到说明被删除了
        if (beforeIndex < 0) {
          delIndex.resultIds.push(id);
        }
      });
      target.personnelKeyResultsIndexLibraryApply?.forEach((keyResult) => {
        if (!tempTarget?.personnelKeyResultsIndexLibrary) return;
        let tempKeyResultIndex =
          tempTarget?.personnelKeyResultsIndexLibrary.findIndex(
            (item) =>
              item.id === keyResult.primitiveId && item.id !== item.primitiveId
          );
        let tempKeyResult =
          tempTarget.personnelKeyResultsIndexLibrary[tempKeyResultIndex];
        if (keyResult && keyResult.operate === 2) {
          tempTarget.personnelKeyResultsIndexLibrary.push({
            ...keyResult,
            id: keyResult.primitiveId
          });
        } else if (keyResult && keyResult.operate === 3) {
          // 修改的时候才对比id
          const beforeIds = tempKeyResult.scorer?.map((item) => item.id);
          beforeIds.forEach((id) => {
            const beforeIndex = keyResult.scorer.findIndex(
              (item) => item.primitiveId === id
            );
            //没有找到说明被删除了
            if (beforeIndex < 0) {
              delScorer.targetIds.push(id);
            }
          });
          keyResult.scorer?.forEach((scorer) => {
            let tempScorerIndex = tempKeyResult.scorer.findIndex(
              (item) =>
                item.id === scorer.primitiveId && item.id !== item.primitiveId
            );
            let tempScorer = tempKeyResult.scorer[tempScorerIndex];
            if (tempScorerIndex < 0) {
              tempKeyResult.scorer.push({
                ...scorer,
                id: scorer.primitiveId
              });
            } else {
              tempKeyResult.scorer[tempScorerIndex] = {
                ...scorer,
                id: scorer.primitiveId
              };
            }
          });
          tempTarget.personnelKeyResultsIndexLibrary[tempKeyResultIndex] = {
            ...keyResult,
            ...tempKeyResult,
            scorerStatus: keyResult.scorerStatus,
            indexWeight: keyResult.indexWeight,
            result: keyResult.result
          };
        } else if (keyResult && keyResult.operate === 4) {
          delIndex.resultIds.push(keyResult.primitiveId);
        }
      });
      tempObj.personnelTargetIndexLibrary[tempTargetIndex] = {
        ...target,
        personnelKeyResultsIndexLibrary:
          tempTarget.personnelKeyResultsIndexLibrary,
        id: target.primitiveId
      };
    } else if (target.operate === 2) {
      target.personnelKeyResultsIndexLibraryApply.forEach((keyResult) => {
        keyResult.id = keyResult.primitiveId;
        keyResult.scorer?.forEach((scorer) => {
          scorer.id = scorer.primitiveId;
        });
      });
      target.id = target.primitiveId;
      tempObj.personnelTargetIndexLibrary.push(target);
    } else if (target.operate === 4) {
      delIndex.targetIds.push(target.primitiveId);
    }
  });
  //操作operate（1 不变，2增加，3修改，4删除）
  tempObj.personnelKeyPerformanceIndexLibraryApply?.forEach((target) => {
    let tempTargetIndex = tempObj.personnelKeyPerformanceIndexLibrary.findIndex(
      (item) => item.id === target.primitiveId && item.id !== item.primitiveId
    );
    let tempTarget =
      tempObj.personnelKeyPerformanceIndexLibrary[tempTargetIndex];
    if (target && target.operate === 3) {
      // 修改的时候才对比id
      const beforeIds = tempTarget.scorer?.map((item) => item.id);
      beforeIds.forEach((id) => {
        const beforeIndex = target.scorer.findIndex(
          (item) => item.primitiveId === id
        );
        //没有找到说明被删除了
        if (beforeIndex < 0) {
          delScorer.keyPerformanceIds.push(id);
        }
      });
      target.scorer?.forEach((scorer) => {
        let tempScorerIndex = tempTarget.scorer.findIndex(
          (item) =>
            item.id === scorer.primitiveId && item.id !== item.primitiveId
        );
        if (tempScorerIndex < 0) {
          tempTarget.scorer.push({
            ...scorer,
            id: scorer.primitiveId
          });
        } else {
          tempTarget.scorer[tempScorerIndex] = {
            ...scorer,
            id: scorer.primitiveId
          };
        }
      });
      tempObj.personnelKeyPerformanceIndexLibrary[tempTargetIndex] = {
        ...target,
        scorer: tempTarget.scorer,
        id: target.primitiveId
      };
    } else if (target.operate === 2) {
      target.scorer?.forEach((scorer) => {
        scorer.id = scorer.primitiveId;
      });
      target.id = target.primitiveId;
      tempObj.personnelKeyPerformanceIndexLibrary.push(target);
    } else if (target.operate === 4) {
      delIndex.keyPerformanceIds.push(target.primitiveId);
    }
  });
  tempObj.personnelWorkPlanIndexLibraryApply?.forEach((target) => {
    let tempTargetIndex = tempObj.personnelWorkPlanIndexLibrary.findIndex(
      (item) => item.id === target.primitiveId && item.id !== item.primitiveId
    );
    let tempTarget = tempObj.personnelWorkPlanIndexLibrary[tempTargetIndex];
    if (target && target.operate === 3) {
      // 修改的时候才对比id
      const beforeIds = tempTarget.scorer?.map((item) => item.id);
      beforeIds.forEach((id) => {
        const beforeIndex = target.scorer.findIndex(
          (item) => item.primitiveId === id
        );
        //没有找到说明被删除了
        if (beforeIndex < 0) {
          delScorer.workPlanIds.push(id);
        }
      });
      target.scorer?.forEach((scorer) => {
        let tempScorerIndex = tempTarget.scorer.findIndex(
          (item) =>
            item.id === scorer.primitiveId && item.id !== item.primitiveId
        );
        if (tempScorerIndex < 0) {
          tempTarget.scorer.push({
            ...scorer,
            id: scorer.primitiveId
          });
        } else {
          tempTarget.scorer[tempScorerIndex] = {
            ...scorer,
            id: scorer.primitiveId
          };
        }
      });
      tempObj.personnelWorkPlanIndexLibrary[tempTargetIndex] = {
        ...target,
        scorer: tempTarget.scorer,
        id: target.primitiveId
      };
    } else if (target.operate === 2) {
      target.scorer?.forEach((scorer) => {
        scorer.id = scorer.primitiveId;
      });
      target.id = target.primitiveId;
      tempObj.personnelWorkPlanIndexLibrary.push(target);
    } else if (target.operate === 4) {
      delIndex.workPlanIds.push(target.primitiveId);
    }
  });
  tempObj.personnelAddSubtractIndexLibraryApply?.forEach((target) => {
    let tempTargetIndex = tempObj.personnelAddSubtractIndexLibrary.findIndex(
      (item) => item.id === target.primitiveId && item.id !== item.primitiveId
    );
    let tempTarget = tempObj.personnelAddSubtractIndexLibrary[tempTargetIndex];
    if (target && target.operate === 3) {
      // 修改的时候才对比id
      const beforeIds = tempTarget.scorer?.map((item) => item.id);
      beforeIds.forEach((id) => {
        const beforeIndex = target.scorer.findIndex(
          (item) => item.primitiveId === id
        );
        //没有找到说明被删除了
        if (beforeIndex < 0) {
          delScorer.addSubtractIds.push(id);
        }
      });
      target.scorer?.forEach((scorer) => {
        let tempScorerIndex = tempTarget.scorer.findIndex(
          (item) =>
            item.id === scorer.primitiveId && item.id !== item.primitiveId
        );
        if (tempScorerIndex < 0) {
          tempTarget.scorer.push({
            ...scorer,
            id: scorer.primitiveId
          });
        } else {
          tempTarget.scorer[tempScorerIndex] = {
            ...scorer,
            id: scorer.primitiveId
          };
        }
      });
      tempObj.personnelAddSubtractIndexLibrary[tempTargetIndex] = {
        ...target,
        scorer: tempTarget.scorer,
        id: target.primitiveId
      };
    } else if (target.operate === 2) {
      target.scorer?.forEach((scorer) => {
        scorer.id = scorer.primitiveId;
      });
      target.id = target.primitiveId;
      tempObj.personnelAddSubtractIndexLibrary.push(target);
    } else if (target.operate === 4) {
      delIndex.addSubtractIds.push(target.primitiveId);
    }
  });
  tempObj.personnelAbilityQualityIndexLibraryApply?.forEach((target) => {
    let tempTargetIndex = tempObj.personnelAbilityQualityIndexLibrary.findIndex(
      (item) => item.id === target.primitiveId && item.id !== item.primitiveId
    );
    let tempTarget =
      tempObj.personnelAbilityQualityIndexLibrary[tempTargetIndex];
    if (target && target.operate === 3) {
      // 修改的时候才对比id
      const beforeIds = tempTarget.scorer?.map((item) => item.id);
      beforeIds.forEach((id) => {
        const beforeIndex = target.scorer.findIndex(
          (item) => item.primitiveId === id
        );
        //没有找到说明被删除了
        if (beforeIndex < 0) {
          delScorer.ablityIds.push(id);
        }
      });
      target.scorer?.forEach((scorer) => {
        let tempScorerIndex = tempTarget.scorer.findIndex(
          (item) =>
            item.id === scorer.primitiveId && item.id !== item.primitiveId
        );
        if (tempScorerIndex < 0) {
          tempTarget.scorer.push({
            ...scorer,
            id: scorer.primitiveId
          });
        } else {
          tempTarget.scorer[tempScorerIndex] = {
            ...scorer,
            id: scorer.primitiveId
          };
        }
      });
      tempObj.personnelAbilityQualityIndexLibrary[tempTargetIndex] = {
        ...target,
        scorer: tempTarget.scorer,
        id: target.primitiveId
      };
    } else if (target.operate === 2) {
      target.scorer?.forEach((scorer) => {
        scorer.id = scorer.primitiveId;
      });
      target.id = target.primitiveId;
      tempObj.personnelAbilityQualityIndexLibrary.push(target);
    } else if (target.operate === 4) {
      delIndex.ablityIds.push(target.primitiveId);
    }
  });
  tempObj.personnelVetoIndexLibraryApply?.forEach((target) => {
    let tempTargetIndex = tempObj.personnelVetoIndexLibrary.findIndex(
      (item) => item.id === target.primitiveId && item.id !== item.primitiveId
    );
    let tempTarget = tempObj.personnelVetoIndexLibrary[tempTargetIndex];
    if (target && target.operate === 3) {
      // 修改的时候才对比id
      const beforeIds = tempTarget.scorer?.map((item) => item.id);
      beforeIds.forEach((id) => {
        const beforeIndex = target.scorer.findIndex(
          (item) => item.primitiveId === id
        );
        //没有找到说明被删除了
        if (beforeIndex < 0) {
          delScorer.veteIds.push(id);
        }
      });
      target.scorer?.forEach((scorer) => {
        let tempScorerIndex = tempTarget.scorer.findIndex(
          (item) =>
            item.id === scorer.primitiveId && item.id !== item.primitiveId
        );
        if (tempScorerIndex < 0) {
          tempTarget.scorer.push({
            ...scorer,
            id: scorer.primitiveId
          });
        } else {
          tempTarget.scorer[tempScorerIndex] = {
            ...scorer,
            id: scorer.primitiveId
          };
        }
      });
      tempObj.personnelVetoIndexLibrary[tempTargetIndex] = {
        ...target,
        scorer: tempTarget.scorer,
        id: target.primitiveId
      };
    } else if (target.operate === 2) {
      target.scorer?.forEach((scorer) => {
        scorer.id = scorer.primitiveId;
      });
      target.id = target.primitiveId;
      tempObj.personnelVetoIndexLibrary.push(target);
    } else if (target.operate === 4) {
      delIndex.veteIds.push(target.primitiveId);
    }
  });
  await probationExamineReview(tempObj);
  if (delScorer.ablityIds.length) {
    await delScoreHuman({ ids: delScorer.ablityIds, type: 1 });
  }
  if (delScorer.keyPerformanceIds.length) {
    await delScoreHuman({ ids: delScorer.keyPerformanceIds, type: 3 });
  }
  if (delScorer.workPlanIds.length) {
    await delScoreHuman({ ids: delScorer.workPlanIds, type: 5 });
  }
  if (delScorer.addSubtractIds.length) {
    await delScoreHuman({ ids: delScorer.addSubtractIds, type: 2 });
  }
  if (delScorer.targetIds.length) {
    await delScoreHuman({ ids: delScorer.targetIds, type: 6 });
  }
  if (delScorer.veteIds.length) {
    await delScoreHuman({ ids: delScorer.veteIds, type: 4 });
  }
  const {
    targetIds,
    addSubtractIds,
    ablityIds,
    veteIds,
    workPlanIds,
    keyPerformanceIds,
    resultIds
  } = delIndex;
  if (resultIds.length) {
    await delKeyResult({ ids: resultIds });
  }
  if (targetIds.length) {
    await delExamineToolInfo({ ids: targetIds, type: 6 });
  }
  if (addSubtractIds.length) {
    await delExamineToolInfo({ ids: addSubtractIds, type: 2 });
  }
  if (ablityIds.length) {
    await delExamineToolInfo({ ids: ablityIds, type: 1 });
  }
  if (veteIds.length) {
    await delExamineToolInfo({ ids: veteIds, type: 4 });
  }
  if (workPlanIds.length) {
    await delExamineToolInfo({ ids: workPlanIds, type: 6 });
  }
  if (keyPerformanceIds.length) {
    await delExamineToolInfo({ ids: keyPerformanceIds, type: 3 });
  }

  reset();
  targetChangeArr2.value = { reason: '' };
  getProbation();
};
const rejectVisible = ref(false);
const rejectChange = () => {
  rejectVisible.value = true;
};
const confirmRejecct = async () => {
  //拒绝变更，去掉新增和修改的数据
  probationDetail.value.optionStatus = 19;
  await probationExamineReview(probationDetail.value);
  getProbation();
  reset();
};
interface Selected {
  targetIds: number[];
  ablityIds: number[];
  addSubtractIds: number[];
  keyPerformanceIds: number[];
  vetoIds: number[];
  workPlanIds: number[];
}
const selected = ref<Selected>({
  targetIds: [],
  ablityIds: [],
  addSubtractIds: [],
  keyPerformanceIds: [],
  vetoIds: [],
  workPlanIds: []
});
const deleted = ref({
  targetIds: [],
  ablityIds: [],
  addSubtractIds: [],
  keyPerformanceIds: [],
  vetoIds: [],
  workPlanIds: [],
  resultIds: []
});
const deletedScorer = ref({
  targetIds: [],
  ablityIds: [],
  addSubtractIds: [],
  keyPerformanceIds: [],
  vetoIds: [],
  workPlanIds: []
});
const rejectReturnProbation = ref<ProbationExamine>({ reason: '' });
// 删除评分人
const submitDelScorer = async () => {
  const {
    ablityIds,
    targetIds,
    addSubtractIds,
    keyPerformanceIds,
    workPlanIds,
    vetoIds
  } = deletedScorer.value;
  if (ablityIds.length) {
    await delScoreHuman({ ids: ablityIds, type: 1 });
  }
  if (targetIds.length) {
    await delScoreHuman({ ids: targetIds, type: 6 });
  }
  if (addSubtractIds.length) {
    await delScoreHuman({ ids: addSubtractIds, type: 2 });
  }
  if (keyPerformanceIds.length) {
    await delScoreHuman({ ids: keyPerformanceIds, type: 3 });
  }
  if (workPlanIds.length) {
    await delScoreHuman({ ids: workPlanIds, type: 5 });
  }
  if (vetoIds.length) {
    await delScoreHuman({ ids: vetoIds, type: 4 });
  }
};
// 删除指标
const delIndex = async () => {
  const {
    ablityIds,
    addSubtractIds,
    workPlanIds,
    targetIds,
    vetoIds,
    keyPerformanceIds
  } = deleted.value;
  if (ablityIds.length) {
    await delExamineToolInfo({ ids: ablityIds, type: 1 });
  }
  if (addSubtractIds.length) {
    await delExamineToolInfo({ ids: addSubtractIds, type: 2 });
  }
  if (workPlanIds.length) {
    await delExamineToolInfo({ ids: workPlanIds, type: 5 });
  }
  if (targetIds.length) {
    await delExamineToolInfo({ ids: targetIds, type: 6 });
  }
  if (vetoIds.length) {
    await delExamineToolInfo({ ids: vetoIds, type: 4 });
  }
  if (keyPerformanceIds.length) {
    await delExamineToolInfo({ ids: keyPerformanceIds, type: 3 });
  }
};
//同意退回并提交新方案
const submitNewExamin = async () => {
  Modal.info({
    title: '确认提交',
    hideCancel: false,
    content: '',
    onCancel: () => {
      console.log('onCancel');
    },
    onOk: async () => {
      indexState2();
      probationDetail.value.optionStatus = 3;
      submitDelScorer();
      delIndex();
      reset();
      await editProbationExamine(probationDetail.value);
      getProbation();
    }
  });
};
const reset = () => {
  returnTemp.value = 0;
  onlyShowSelected.value = false;
  tabIndex.value = 1;
  selected.value = {
    targetIds: [],
    ablityIds: [],
    addSubtractIds: [],
    keyPerformanceIds: [],
    vetoIds: [],
    workPlanIds: []
  };
  deleted.value = {
    targetIds: [],
    ablityIds: [],
    addSubtractIds: [],
    keyPerformanceIds: [],
    vetoIds: [],
    workPlanIds: [],
    resultIds: []
  };
  deletedScorer.value = {
    targetIds: [],
    ablityIds: [],
    addSubtractIds: [],
    keyPerformanceIds: [],
    vetoIds: [],
    workPlanIds: []
  };
};
const rejectReturnVisible = ref(false);
// 拒绝退回数据state改为2
const indexState = () => {
  const {
    personnelAbilityQualityIndexLibrary,
    personnelAbilityQualityIndexLibraryApply,
    personnelAddSubtractIndexLibrary,
    personnelAddSubtractIndexLibraryApply,
    personnelKeyPerformanceIndexLibrary,
    personnelKeyPerformanceIndexLibraryApply,
    personnelTargetIndexLibrary,
    personnelTargetIndexLibraryApply,
    personnelVetoIndexLibrary,
    personnelVetoIndexLibraryApply,
    personnelWorkPlanIndexLibrary,
    personnelWorkPlanIndexLibraryApply
  } = rejectReturnProbation.value;
  personnelAbilityQualityIndexLibrary.forEach((item) => (item.state = 0));
  personnelAbilityQualityIndexLibraryApply.forEach((item) => (item.state = 0));
  personnelAddSubtractIndexLibrary.forEach((item) => (item.state = 0));
  personnelAddSubtractIndexLibraryApply.forEach((item) => (item.state = 0));
  personnelKeyPerformanceIndexLibrary.forEach((item) => (item.state = 0));
  personnelKeyPerformanceIndexLibraryApply.forEach((item) => (item.state = 0));
  personnelTargetIndexLibrary.forEach((item) => (item.state = 0));
  personnelTargetIndexLibraryApply.forEach((item) => (item.state = 0));
  personnelVetoIndexLibrary.forEach((item) => (item.state = 0));
  personnelVetoIndexLibraryApply.forEach((item) => (item.state = 0));
  personnelWorkPlanIndexLibrary.forEach((item) => (item.state = 0));
  personnelWorkPlanIndexLibraryApply.forEach((item) => (item.state = 0));
};
// 同意退回数据state改为2
const indexState2 = () => {
  const {
    personnelAbilityQualityIndexLibrary,
    personnelAbilityQualityIndexLibraryApply,
    personnelAddSubtractIndexLibrary,
    personnelAddSubtractIndexLibraryApply,
    personnelKeyPerformanceIndexLibrary,
    personnelKeyPerformanceIndexLibraryApply,
    personnelTargetIndexLibrary,
    personnelTargetIndexLibraryApply,
    personnelVetoIndexLibrary,
    personnelVetoIndexLibraryApply,
    personnelWorkPlanIndexLibrary,
    personnelWorkPlanIndexLibraryApply
  } = probationDetail.value;
  personnelAbilityQualityIndexLibrary.forEach((item) => (item.state = 0));
  personnelAbilityQualityIndexLibraryApply.forEach((item) => (item.state = 0));
  personnelAddSubtractIndexLibrary.forEach((item) => (item.state = 0));
  personnelAddSubtractIndexLibraryApply.forEach((item) => (item.state = 0));
  personnelKeyPerformanceIndexLibrary.forEach((item) => (item.state = 0));
  personnelKeyPerformanceIndexLibraryApply.forEach((item) => (item.state = 0));
  personnelTargetIndexLibrary.forEach((item) => (item.state = 0));
  personnelTargetIndexLibraryApply.forEach((item) => (item.state = 0));
  personnelVetoIndexLibrary.forEach((item) => (item.state = 0));
  personnelVetoIndexLibraryApply.forEach((item) => (item.state = 0));
  personnelWorkPlanIndexLibrary.forEach((item) => (item.state = 0));
  personnelWorkPlanIndexLibraryApply.forEach((item) => (item.state = 0));
};
// 退回结果
const rejectReturn = async () => {
  indexState();
  rejectReturnProbation.value.optionStatus = 21;
  reset();
  await editProbationExamine(rejectReturnProbation.value);
  getProbation();
};
const confirmResultVisible = ref(false);
//确认结果
const confirmResult = async () => {
  Modal.info({
    title: '确认提交',
    hideCancel: false,
    content: '',
    onCancel: () => {
      console.log('onCancel');
    },
    onOk: async () => {
      const { id, resultConfirmeTime } = probationDetail.value;
      const obj = { id, resultConfirmeTime, optionStatus: 11 };
      await probationResultConfirm(obj);
      getProbation();
    }
  });
};
/**
 * 退回结果,退回到自评
 */
const returnScoreResult = async () => {
  probationDetail.value.optionStatus = 8;
  // 清空分数、说明
  probationDetail.value.personnelKeyPerformanceIndexLibrary?.forEach((item) => {
    item.scorer.forEach((scorer) => {
      scorer.operator = 0;
      scorer.note = '';
      scorer.score = 0;
    });
  });
  probationDetail.value.personnelAbilityQualityIndexLibrary?.forEach((item) => {
    item.scorer?.forEach((scorer) => {
      scorer.operator = 0;
      scorer.note = '';
      scorer.score = 0;
    });
  });
  probationDetail.value.personnelAddSubtractIndexLibrary?.forEach((item) => {
    item.scorer.forEach((scorer) => {
      scorer.operator = 0;
      scorer.note = '';
      scorer.score = 0;
    });
  });
  probationDetail.value.personnelTargetIndexLibrary?.forEach((item) => {
    item.personnelKeyResultsIndexLibrary?.forEach((result) => {
      result.scorer.forEach((scorer) => {
        scorer.operator = 0;
        scorer.note = '';
        scorer.score = 0;
      });
    });
  });
  probationDetail.value.personnelWorkPlanIndexLibrary?.forEach((item) => {
    item.scorer.forEach((scorer) => {
      scorer.operator = 0;
      scorer.note = '';
      scorer.score = 0;
    });
  });
  probationDetail.value.personnelVetoIndexLibrary?.forEach((item) => {
    item.scorer.forEach((scorer) => {
      scorer.operator = 0;
      scorer.note = '';
      scorer.score = 0;
      scorer.whetherViolate = false;
    });
  });
  reset();
  await editProbationExamine(probationDetail.value);
  getProbation();
};
const resultAppeal = () => {
  showRowSelection.value = !showRowSelection.value;
  appealStemp.value++;
};
const cancelAppeal = () => {
  showRowSelection.value = !showRowSelection.value;
  appealStemp.value--;
  showAppeal.value = false;
};
const appealData = ref<any>({
  id: 0,
  optionStatus: 0,
  personnelTargetIndexLibrary: []
});
const showAppeal = ref(false);
// 确认申诉
const confirmAppeal = () => {
  appealData.value = cloneDeep(probationDetail.value);
  appealData.value.personnelTargetIndexLibrary = [];
  appealData.value.personnelAbilityQualityIndexLibrary = [];
  appealData.value.personnelAddSubtractIndexLibrary = [];
  appealData.value.personnelWorkPlanIndexLibrary = [];
  appealData.value.personnelKeyPerformanceIndexLibrary = [];
  appealData.value.personnelVetoIndexLibrary = [];
  if (selected.value.targetIds.length) {
    selected.value.targetIds.forEach((targetId) => {
      const targetItem =
        probationDetail.value.personnelTargetIndexLibrary?.find(
          (target) => target.id === targetId
        );
      if (!targetItem) return;
      appealData.value.personnelTargetIndexLibrary.push({
        ...targetItem,
        state: 4
      });
    });
  }
  if (selected.value.ablityIds.length) {
    selected.value.ablityIds.forEach((targetId) => {
      const targetItem =
        probationDetail.value.personnelAbilityQualityIndexLibrary?.find(
          (target) => target.id === targetId
        );
      if (!targetItem) return;
      appealData.value.personnelAbilityQualityIndexLibrary.push({
        ...targetItem,
        state: 4
      });
    });
  }
  if (selected.value.addSubtractIds.length) {
    selected.value.addSubtractIds.forEach((targetId) => {
      const targetItem =
        probationDetail.value.personnelAddSubtractIndexLibrary?.find(
          (target) => target.id === targetId
        );
      if (!targetItem) return;
      appealData.value.personnelAddSubtractIndexLibrary.push({
        ...targetItem,
        state: 4
      });
    });
  }
  if (selected.value.workPlanIds.length) {
    selected.value.workPlanIds.forEach((targetId) => {
      const targetItem =
        probationDetail.value.personnelWorkPlanIndexLibrary?.find(
          (target) => target.id === targetId
        );
      if (!targetItem) return;
      appealData.value.personnelWorkPlanIndexLibrary.push({
        ...targetItem,
        state: 4
      });
    });
  }
  if (selected.value.keyPerformanceIds.length) {
    selected.value.keyPerformanceIds.forEach((targetId) => {
      const targetItem =
        probationDetail.value.personnelKeyPerformanceIndexLibrary?.find(
          (target) => target.id === targetId
        );
      if (!targetItem) return;
      appealData.value.personnelKeyPerformanceIndexLibrary.push({
        ...targetItem,
        state: 4
      });
    });
  }
  if (selected.value.vetoIds.length) {
    selected.value.vetoIds.forEach((targetId) => {
      const targetItem = probationDetail.value.personnelVetoIndexLibrary?.find(
        (target) => target.id === targetId
      );
      if (!targetItem) return;
      appealData.value.personnelVetoIndexLibrary.push({
        ...targetItem,
        state: 4
      });
    });
  }
  // console.log(appealData.value);
  showRowSelection.value = !showRowSelection.value;
  showAppeal.value = true;
  appealStemp.value++;
};
const appealStemp = ref(0);
// 员工提交申诉 state=4
const submitAppeal = async () => {
  Modal.info({
    title: '确认提交',
    hideCancel: false,
    content: '',
    onCancel: () => {
      console.log('onCancel');
    },
    onOk: async () => {
      appealData.value.optionStatus = 15;
      await probationResultAppeal(appealData.value);
      getProbation();
      showAppeal.value = false;
      appealStemp.value = 0;
    }
  });
};
//考核申诉处理state=5同意=6驳回
const submitAppealHandle = async () => {
  Modal.info({
    title: '确认提交',
    hideCancel: false,
    content: '',
    onCancel: () => {
      console.log('onCancel');
    },
    onOk: async () => {
      await probationResultAppealOperate(appealHandleData.value);
      getProbation();
    }
  });
};
const userId = Number(localStorage.getItem('userId'));
// 方案待启动，是否显示操作按钮
const showReturnButton = computed(() => {
  if (userId !== probationDetail.value.userId) return false;
  if (![3, 21].includes(probationDetail.value.optionStatus)) return false;
  return true;
});
const args = computed(() => {
  const { optionStatus, userId: probationUserId } = probationDetail.value;
  const tblArgs = {
    showAdd: false,
    showOperation: false,
    scorerType: 1,
    showScoreInput: false,
    showReason: false,
    showIndexScore: false,
    showAppealHandel: false,
    showIsPass: false,
    showWeight: false
  };

  // 退回审核
  if (optionStatus === 4) {
    tblArgs.showReason = true;
    // 先查看是否方案所属者
    if (userId !== probationUserId) {
      tblArgs.showAdd = true;
      tblArgs.showOperation = true;
    }
  }
  // 自评
  if ([8, 12].includes(optionStatus)) {
    tblArgs.showScoreInput = true;
    // 自评不显示他评

    // 他评互相不显示
  }
  // 他评
  if ([9, 13].includes(optionStatus)) {
    tblArgs.showScoreInput = true;
    // 自评不显示他评

    // 他评互相不显示
  }
  if (optionStatus >= 10 && optionStatus !== 21) {
    tblArgs.showIndexScore = true;
  }
  if (optionStatus === 15) {
    tblArgs.showAppealHandel = true;
  }
  if (optionStatus === 16) {
    tblArgs.showIsPass = true;
  }
  return tblArgs;
});
// 提交新方案或拒绝按钮
const submitOrRejectButton = computed(() => {
  if (userId === probationDetail.value.userId) return false;
  if (probationDetail.value?.optionStatus !== 4) return false;
  return true;
});
// 评分按钮
const showScoreButton = computed(() => {
  const { optionStatus, userId: probationUserId } = probationDetail.value;
  if ([8, 12].includes(optionStatus) && userId === probationUserId) {
    return true;
  }
  // 考核评分人是否包含登陆者
  if ([9, 13].includes(optionStatus) && currentUserIsScorer.value) {
    return true;
  }
  return false;
});
// 申请变更按钮
const showApplyChangeButton = computed(() => {
  const { optionStatus, userId: probationUserId } = probationDetail.value;
  if ([5, 19, 20].includes(optionStatus)) return true;

  return false;
});
// 申请变更后的处理按钮
const showChangeHandleButton = computed(() => {
  const { optionStatus, userId: probationUserId } = probationDetail.value;
  if (optionStatus === 6 && userId !== probationUserId) return true;
  if (optionStatus === 7 && userId === probationUserId) return true;

  return false;
});
</script>
<style lang="less" scoped>
.detail-card {
  box-shadow: 0px 0px 2px rgba(0, 0, 0, 0.12);
  display: flex;
  flex-direction: column;

  .card-top {
    display: flex;
    padding: 16px;
    .card-top-center {
      flex: 1;
      padding: 0 16px;
    }
  }
  .card-bottom {
    padding: 16px;
  }
}
.btn-group {
  width: 100%;
  display: flex;
  justify-content: center;
  padding: 10px 0;
  // border-top: 2px solid #f8f8f8;
  // box-shadow: 0px -6px 2px rgb(0 0 0 / 2%);
}
.card-top-right {
  display: flex;
  border: 1px solid #eee;
  padding: 5px;
  border-radius: 2px;
}
.small-circle {
  width: 70px;
  height: 70px;
  border-radius: 50%;
  background-color: #fff;
  border: 2px solid green;
  border-top: 2px solid #fff;
  border-right: 2px solid #fff;
  position: absolute;
  box-sizing: border-box;
  bottom: 5px;
  left: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
  .confirmed {
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
.big-circle {
  // box-sizing: border-box;
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background-color: #fff;
  border: 3px solid green;
  border-top: 3px solid #fff;
  border-right: 3px solid #fff;
  position: relative;
}
</style>
